﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace Shileme.Common.Helper
{
    public class TypeConvert
    {

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(object expression, bool defValue)
        {
            if (expression != null)
                return StrToBool(expression, defValue);

            return defValue;
        }

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(string expression, bool defValue)
        {
            if (expression != null)
            {
                if (string.Compare(expression, "true", true) == 0)
                    return true;
                else if (string.Compare(expression, "false", true) == 0)
                    return false;
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression)
        {
            return ObjectToInt(expression, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression, int defValue)
        {
            if (expression != null)
                return StrToInt(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 转换为string类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static string ObjectToStr(object expression)
        {
            if (expression != null)
                return expression.ToString();
            return "";
        }

        /// <summary>
        /// 将对象转换为Int32类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str)
        {
            return StrToInt(str, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (Int32.TryParse(str, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(str, defValue));
        }

        /// <summary>
        /// 将long型数值转换为Int32类型
        /// </summary>
        /// <param name="objNum">要转换的对象</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static int SafeInt32(object objNum, int defValue)
        {
            if (objNum == null)
                return defValue;

            string strNum = objNum.ToString();
            if (Validator.IsNumeric(strNum))
            {
                if (strNum.ToString().Length > 9)
                {
                    if (strNum.StartsWith("-"))
                        return int.MinValue;
                    else
                        return int.MaxValue;
                }
                return Int32.Parse(strNum);
            }
            else
                return defValue;
        }

        /// <summary>
        /// 整型数转成字符串
        /// </summary>
        /// <param name="intValue">要转换的对象</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的string类型结果</returns>
        public static string IntToStr(int intValue, string defValue)
        {
            try
            {
                return Convert.ToString(intValue);
            }
            catch
            {
                return defValue;
            }
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(object strValue, float defValue)
        {
            if ((strValue == null))
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float ObjectToFloat(object strValue, float defValue)
        {
            if ((strValue == null))
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float ObjectToFloat(object strValue)
        {
            return ObjectToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue)
        {
            if ((strValue == null))
                return 0;

            return StrToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(strValue, out intValue);
            }
            return intValue;
        }

        /// <summary>
        /// string型转换为Decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的Decimal类型结果</returns>
        public static Decimal StrToDecimal(object strValue, Decimal defValue)
        {
            if ((strValue == null) || (strValue.ToString().Length > 10))
                return defValue;

            Decimal intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    Decimal.TryParse(strValue.ToString(), out intValue);
            }
            return intValue;
        }

        /// <summary>
        /// string型转换为Decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的Decimal类型结果</returns>
        public static Decimal StrToDecimal(object strValue)
        {
            return StrToDecimal(strValue, 0);
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime StrToDateTime(string str, DateTime defValue)
        {
            if (!string.IsNullOrEmpty(str))
            {
                DateTime dateTime;
                if (DateTime.TryParse(str, out dateTime))
                    return dateTime;
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime StrToDateTime(string str)
        {
            return StrToDateTime(str, DateTime.Now);
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime ObjectToDateTime(object obj)
        {
            return StrToDateTime(obj.ToString());
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime ObjectToDateTime(object obj, DateTime defValue)
        {
            return StrToDateTime(obj.ToString(), defValue);
        }

        /// <summary>
        /// 字符串转成整型数组
        /// </summary>
        /// <param name="idList">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int[] StringToIntArray(string idList)
        {
            return StringToIntArray(idList, -1);
        }

        /// <summary>
        /// 字符串转成整型数组
        /// </summary>
        /// <param name="idList">要转换的字符串</param>
        /// <param name="defValue">默认值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int[] StringToIntArray(string idList, int defValue)
        {
            if (string.IsNullOrEmpty(idList))
                return null;
            string[] strArr = Utils.SplitString(idList, ",");
            int[] intArr = new int[strArr.Length];
            for (int i = 0; i < strArr.Length; i++)
                intArr[i] = StrToInt(strArr[i], defValue);

            return intArr;
        }

        /// <summary>
        /// 根据字符串获取枚举值
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="value">字符串枚举值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static T StrToEnum<T>(string value, T defValue)
        {
            try
            {
                return (T)System.Enum.Parse(typeof(T), value, true);
            }
            catch (ArgumentException)
            {
                return defValue;
            }
        }

        /// <summary>
        /// 得到一个新的ID
        /// </summary>
        /// <param name="obj">当前对象</param>
        /// <returns></returns>
        public static int GetNewId(string obj)
        {
            int rId = 1;
            Int32.TryParse(obj, out rId);
            if (rId == 0)
                rId = 1;

            return rId;
        }


        ////简体转繁体
        //public static string SimpToTrad(string Str)
        //{
        //    return Microsoft.VisualBasic.Strings.StrConv(Str, VbStrConv.TraditionalChinese,0);
        //}
        ////繁体转简体
        //public static string TradToSimp(string Str)
        //{
        //    return Microsoft.VisualBasic.Strings.StrConv(Str, VbStrConv.SimplifiedChinese,0);
        //}
        //判断是繁体还是简体
        //public bool IsSimp(String Str)
        //{
        //    //byte[] bytes = Encoding.GetEncoding("BIG5").GetBytes(Str);
        //    //if(bytes<1)
        //    //{

        //    //}
        //}

        //#region 反射 DT转list<Entity>  --Pejon
        //public static List<T> DT2ModelList<T>(DataTable dt)
        //{
        //    if (dt == null || dt.Rows.Count == 0)
        //    {
        //        return null;
        //    }

        //    List<T> entityList = new List<T>();
        //    T entity = default(T);
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        entity = Activator.CreateInstance<T>();
        //        PropertyInfo[] pis = entity.GetType().GetProperties();
        //        foreach (PropertyInfo pi in pis)
        //        {
        //            if (dt.Columns.Contains(pi.Name))
        //            {
        //                if (!pi.CanWrite)
        //                {
        //                    continue;
        //                }
        //                if (dr[pi.Name] != DBNull.Value)
        //                {
        //                    //真实名，手机，邮箱，身份证都解密显示
        //                    if (pi.Name == "RealName" || pi.Name == "MemMail" || pi.Name == "TelePhone" || pi.Name == "MobilePhone")
        //                    {
        //                        if (dr[pi.Name].ToString() != "")
        //                        {
        //                            try
        //                            {
        //                                pi.SetValue(entity, (object)DEncrypt.Decrypt(dr[pi.Name].ToString(), "ezagoo9&*9"), null);
        //                            }
        //                            catch
        //                            {
        //                                pi.SetValue(entity, dr[pi.Name], null);
        //                            }
        //                        }
        //                        else
        //                        {
        //                            pi.SetValue(entity, dr[pi.Name], null);
        //                        }
        //                    }
        //                    else
        //                    {
        //                        pi.SetValue(entity, dr[pi.Name], null);
        //                    }
        //                }
        //            }
        //        }
        //        entityList.Add(entity);
        //    }
        //    return entityList;
        //}
        //#endregion

    }
}
